//
//  ViewController.m
//  Demo
//
//  Created by HandeIdealVision on 2017/11/6.
//  Copyright © 2017年 HandeIdealVison. All rights reserved.
//

#import "Demo1ViewController.h"
#import <HandeAR/HandeAR.h>
#import <HandeAR/HandeArView.h>

#define ScreenWidth  CGRectGetWidth([UIScreen mainScreen].bounds)
#define ScreenHeight CGRectGetHeight([UIScreen mainScreen].bounds)
#define ZYHeight(height) (ScreenWidth == 375?(height):(height)*ScreenWidth/375)

@interface Demo1ViewController ()<HandeARDelegate, UITabBarDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint;
@property (nonatomic, strong) HandeARView *arView;
@end

@implementation Demo1ViewController

- (id)init {
    
    self = [super init];
    if (self) {
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"Demo";
    self.edgesForExtendedLayout = UIRectEdgeAll;
    self.view.backgroundColor = [UIColor blackColor];
//    [UIView setAnimationsEnabled:YES];
    [HandeAR setDelegate:self];
    [HandeAR setLoginStatus:YES];
    [HandeAR setPublishTime:@"2017-12-11 12:01:01"];
//    [HandeAR setARResourceURL:@"http://47.92.143.17/media/images/"];
    [HandeAR setARResourceURL:@"http://oz513xipu.bkt.clouddn.com/"];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"annualreport" withExtension:@"json"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    [HandeAR setAnnualReport:str];
    [HandeAR setLayoutTopHeight:64 bottomHeight:100];
    
    CGRect bound = [UIScreen mainScreen].bounds;
//    HandeARView *arView = [[HandeARView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-ZYHeight(150.0f))];
    HandeARView *arView = [[HandeARView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    self.arView = arView;
    [self.view addSubview:self.arView];
    [self.view sendSubviewToBack:self.arView];

//    NSDictionary *viewDic = NSDictionaryOfVariableBindings(arView);
//    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(0)-[arView]-(0)-|" options:0 metrics:nil views:viewDic]];
//    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(0)-[arView]-(0)-|" options:0 metrics:nil views:viewDic]];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)dealloc {
    
}

- (IBAction)clearAR:(id)sender {
    NSArray *pathcaches=NSSearchPathForDirectoriesInDomains(NSCachesDirectory
                                                            , NSUserDomainMask
                                                            , YES);
    NSString* cacheDirectory  = [pathcaches objectAtIndex:0];
    NSString *path = [cacheDirectory stringByAppendingPathComponent:@"arasset_ios"];
    NSFileManager *ftm = [NSFileManager defaultManager];
    if ([ftm fileExistsAtPath:path]) {
        [ftm removeItemAtPath:path error:nil];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - HandeARDelegate
//触发分享
- (void)triggerShareAction:(NSData *)imageData {
    //开始分享
}

//识别到AR
- (void)didOnFoundAR {
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self visiableToolBar:NO];
}

//将要回到AR扫一扫
- (void)willShowARScan {
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self visiableToolBar:YES];
}

//识别到二维码
- (void)didOnFoundQR:(NSString *)message {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"二维码" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

- (void)onUnityException:(int)errorCode {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络错误" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    [self.arView resumeQR];
}

- (void)visiableToolBar:(BOOL)visiable {
    if (visiable) {
        [UIView animateWithDuration:0.25 animations:^{
            self.bottomConstraint.constant = 0;
            [self.view layoutIfNeeded];
        } completion:^(BOOL finished) {
            self.arView.frame = CGRectMake(0, 0, ScreenWidth,  ScreenHeight-ZYHeight(150.0f));
        }];
    }
    else {
        [UIView animateWithDuration:0.25 animations:^{
            self.bottomConstraint.constant = -150;
            [self.view layoutIfNeeded];
        } completion:^(BOOL finished) {
            self.arView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        }];
    }
}


- (IBAction)showAR:(id)sender {
    [self.arView showType:AR_Type];
}

- (IBAction)showQR:(id)sender {
    [self.arView showType:QR_Type];
}


@end
